# _*_ coding: utf-8 _*_
# @Time : 2024-01-15 21:55
# @Author : wf
import urllib.request as request
import urllib.parse

base_url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
data = {
    'from': 'en',
    'to': 'zh',
    'query': 'algorithm',
    'transtype': 'realtime',
    'simple_means_flag': '3',
    'sign': '688464.942689',
    'token': '0c0d5a129a3fc468070703bba1e2b3d7',
    'domain': 'common',
    'ts': '1705328002854',
}
data = urllib.parse.urlencode(data).encode('utf-8')

# query_data = 'from=en&to=zh&query=algorithm&transtype=realtime&simple_means_flag=3&sign=688464.942689&token=0c0d5a129a3fc468070703bba1e2b3d7&domain=common&ts=1705328002854'
# query_data = urllib.parse.urlencode(query_data).encode('utf-8')


# headers没有 cookie，返回错误
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}
# 关键字传参
req = request.Request(base_url, data=data, headers=headers)
response = request.urlopen(req)
content = response.read().decode("utf8")
print(content)

import json

data = json.loads(content)
print(data)

print(json.dumps(data, ensure_ascii=False))
